package com.wwp.tbshop.model.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wwp.tbshop.R;
import com.wwp.tbshop.activity.HotProductsActivity;
import com.wwp.tbshop.activity.ProductDetailActivity;
import com.wwp.tbshop.model.bean.HotProductBean;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.UiUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ${wenping} on 6/16/2017.
 */

public class HotProductRVadapter extends RecyclerView.Adapter {

    private Context mContext;
    private HotProductsActivity mHotProductsActivity;

    public HotProductRVadapter(Context context, HotProductsActivity hotProductsActivity) {
        mContext = context;
        mHotProductsActivity = hotProductsActivity;
    }

    private List<HotProductBean.ProductListBean> mProductListBeanList;

    public void setProductListBeanList(List<HotProductBean.ProductListBean> productListBeanList) {
        mProductListBeanList = productListBeanList;
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_hot_product, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(mProductListBeanList.get(position));
    }

    @Override
    public int getItemCount() {
        if (mProductListBeanList==null) {
            return 0;
        }
        return mProductListBeanList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.item_item_hot_product_iv)
        ImageView mItemItemHotProductIv;
        @Bind(R.id.item_hot_product_name)
        TextView mItemHotProductName;
        @Bind(R.id.item_hot_product_marketprice)
        TextView mItemHotProductMarketprice;
        @Bind(R.id.item_hot_product_price)
        TextView mItemHotProductPrice;
        private HotProductBean.ProductListBean mProductListBean;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pId = mProductListBean.getId();
                    Intent intent = new Intent(mContext, ProductDetailActivity.class);
                    intent.putExtra("pId",pId);
                    mContext.startActivity(intent);
                }
            });
        }

        public void setData(HotProductBean.ProductListBean bean) {
            mProductListBean = bean;
            Picasso.with(UiUtil.getContext())
                    .load(Constant.HOST+bean.getPic())
                    .placeholder(R.drawable.pic_default_grid)
                    .error(R.drawable.ic_error_page)
                    .into(mItemItemHotProductIv);
            mItemHotProductName.setText(bean.getName()+"");
            mItemHotProductMarketprice.setText("市价:¥"+bean.getMarketPrice()+"");
            mItemHotProductMarketprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            mItemHotProductPrice.setTextColor(Color.RED);
            mItemHotProductPrice.setText("现价:¥"+bean.getPrice()+"");
        }
    }
}
